<?php

require_once('BaseModel.php');

class Release_model extends BaseModel {

    function Release_model()
    {
        $publicdata = array(
			'version',
			'branch',
			'release_note',
			'rlshash',
			'url',
			'status',
			'type');
        parent::BaseModel('cr_releases', 'rls_id', $publicdata);
    }    
    
    /**
     * Loads a release based on his id and the branch's id
     * Returns true on success, false otherwise
     */
    function load_release_from_id($rlsid) {
    	$this->db->where('rls_id', $rlsid);
    	return $this->load();
    }

    
    /**
     * Loads a release based on his id and the branch's id
     * Returns true on success, false otherwise
     */
    function load_release_from_branch($branch, $version) {
    	$this->db->where('branch', $branch);
    	$this->db->where('version', $version);
    	return $this->load();
    }
    
    /**
     * Creates a new package using its infos
     * Returns  true if the release was successfully created
     * 			an array mapping error messages onto bad fields if it failed 
     */
    function new_release($infos, $branch) {
    	$checkeddata = array();
		$checkeddata['branch'] = $branch;
    	
    	//checks whether this version is already in use
    	if ($this->exists(array('version' => $infos['version'], 'branch' => $branch)))
    		return array('version' => 'This version is already used in this branch');
    	if ($this->exists(array('type' => $infos['type'], 'branch' => $branch))) {
    		//move current release to the historic
    		//deleting to prevent sql errors
    		$this->db->delete('cr_releases', array('type' => $infos['type'], 'branch' => $branch));
    		//return array('type' => 'There is already a release of this type in this branch');
    	}

    	$result = parent::new_data($infos, $checkeddata);
    	if (is_array($result))
    		return $result;
        
        //reloading the release from its branch and version, thus getting his id and complementary infos
        $this->load_release_from_branch($this->data['branch'], $this->data['version']);
        return true;
    }
    
	/**
	 * Stores one release's information in the database
	 * Returns true if the infos got saved, false if an error occured
	 */
	function save_release($rlsid, $data) {
		return $this->save_data($rlsid, $data);
	}
	
	/**
	 * Checks the validity of the infos stored in the $this->data array
	 * Returns true if each field is valid, false otherwise
	 */
	public function fields_are_valid($data) {
		$errormet = false;
		$errors = array();
		foreach ($data as $key => $value) {
			if (isset($value) && strlen($value) == 0) {
				$errors[$key] = $key.' cannot be empty';
				$errormet = true;
			}
		}
		if (isset($data['version']) && strlen($data['version']) > 20) {
			$errors['version'] = 'Version is too long';
			$errormet = true;
		}
		if (isset($data['rlshash']) && strlen($data['rlshash']) > 32) {
			$errors['rlshash'] = 'Hash is invalid (oO)';
			$errormet = true;
		}
		if (isset($data['url']) && strlen($data['url']) > 200) {
			$errors['url'] = 'URL is too long';
			$errormet = true;
		}
		if (isset($data['status']) && !in_array($data['status'], $this->statuses)) {
			$errors['status'] = 'Status is invalid';
			$errormet = true;
		}
		if ($errormet)
			return $errors;
		return true;
	}
	
	/**
	 * Returns the array of all the releases attached to $branch
	 * If $count is null, returns all values
	 * This array is sorted on the $sorted column
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_releases($branch, $first = null, $count = null, $match = null, $sorted = 'version') {
		$this->db->where('branch', $branch);
		return $this->get_data($first, $count, $sorted, $match);
	}
        
    /**
     * Returns the number of releases
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_release_count($match = null) {
		return $this->get_data_count($match);
	}
	
	/**
	 * Returns whether the user $user_id is responsible for this branch
	 */
	function is_responsible($user_id, $rls_id) {
		if (is_numeric($user_id) && is_numeric($rls_id)) {
			$query = $this->db->query('SELECT resp_usr FROM cr_packages WHERE pkg_id IN (' .
					'SELECT package FROM cr_branches WHERE branch_id IN(' .
					'SELECT branch FROM cr_releases WHERE rls_id = '.$rls_id.'))');
			$array = $query->row_array();
			return $user_id == $array['resp_usr'];
		}
	}
}

?>
